import userInfoList from "/util/data"
App({
  userInfoList: userInfoList,
  onLaunch(options) {
    my.getAuthCode({
      scopes: 'auth_user',
      success: () => {
        my.getAuthUserInfo({
          success: (userInfo) => {
            userInfoList.push({
              name: userInfo.nickName === '' ? '你' : userInfo.nickName,
              id: userInfoList.length
            });
            return this.userInfoList = userInfoList;
          }
        });
      },
    });
  },
  /**
   * formatTime 转换时间格式
   * @param {string} date 时间字符串
   */
  formatTime(date) {
    return date.substring(0, 19).replace(new RegExp(/T/gm) ," ");
  },
  /**
   * mapList map获奖列表数据
   * @param {array} list 
   */
  mapList(list) {
    userInfoList.map((val) => {
      list.map((item) => {
        if(val.id.toString() === item.id.toString()) {
          if(item.id === userInfoList.length-1) {
            item.self = true;
          }
          item.name = val.name
          item.createTime = this.formatTime(item.createTime)
        }
      })
    })
    return list
  },
  onLottery() {
    return new Promise((resolve, reject) => {
      my.httpRequest({
        //http://localhost:8080/api/lottery?number=12
        //可以用server的代码本地起服务
        url: 'http://mychain01.inc.alipay.net:8189/api/lottery?number=' + (this.userInfoList.length),
        method: 'POST',
        dataType: 'json',
        success: (res) => {
          resolve(res.data || [])
        },
        fail: (res) => {
          my.hideLoading();
          my.alert({
            content: JSON.stringify(res)
          });
          reject()
        },
      });
    })
  }
});
